package com.huawei123.hasee.cniao5store.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.huawei123.hasee.cniao5store.base.mvp_base.MvpBasePresenter;
import com.huawei123.hasee.cniao5store.base.mvp_base.MvpBaseView;
import com.huawei123.hasee.cniao5store.di.component.ActivityComponent;
import com.huawei123.hasee.cniao5store.di.component.DaggerActivityComponent;
import com.huawei123.hasee.cniao5store.di.module.ActivityModule;

/**
 * 项目名称：Cniao5Store
 * 类描述：Actyivity实现MVP的基类
 * 创建人：Administrator
 * 创建时间：2017/11/12 0012 上午 10:30
 * 修改人：Administrator
 * 修改时间：2017/11/12 0012 上午 10:30
 * 修改备注：首先初始化ActivityComponent，然后使用子类从写抽象方法initInjector()返回的子类的Presenter，绑定View，
 */

public abstract class BaseMvpActivity<T extends MvpBasePresenter> extends BaseActivity implements MvpBaseView {
    //通过Dagger2注入的是  Presenter
    protected ActivityComponent mActivityComponent;
    protected T myPresenter;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActivityComponent();
        //根据子类实现抽象方法返回其Presenter
        myPresenter = initPresenterInjector();
        //子Presenter绑定View
        myPresenter.attachView(this);
        initData();
    }

    /**
     * 初始化ActivityComponent
     */
    private void initActivityComponent() {
        mActivityComponent = DaggerActivityComponent.builder().activityModule(new ActivityModule(this)).appComponent(((StoreApplication)
                getApplication())
                .getAppComponent()).build();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myPresenter != null) {
            myPresenter.detachView(); //子Presenter解绑View
        }
    }

    @Override
    public void showToast(String mag) {
        super.showToast(mag);
    }

    /**
     * 供子类重写返回其Presenter的方法
     *
     * @return T 子类的Presenter
     */
    protected abstract T initPresenterInjector();
}
